/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "HMatrix4x4.h"
#include "HVector3.h"
#include "HVector4.h"

namespace hhd
{
	HMatrix4x4::HMatrix4x4(void)
	{
		_xbase = _mm_setr_ps( 1.0f, 0.0f, 0.0f, 0.0f );
		_ybase = _mm_setr_ps( 0.0f, 1.0f, 0.0f, 0.0f );
		_zbase = _mm_setr_ps( 0.0f, 0.0f, 1.0f, 0.0f );
		_translation = _mm_setr_ps( 0.0f, 0.0f, 0.0f, 1.0f );
	}


	HMatrix4x4::~HMatrix4x4(void)
	{
	}

	HMatrix4x4::HMatrix4x4( const HMatrix4x4& other )
	{
		_xbase = other._xbase;
		_ybase = other._ybase;
		_zbase = other._zbase;
		_translation = other._translation;
	}

	HMatrix4x4::HMatrix4x4( hfloat32 r0c0, hfloat32 r0c1, hfloat32 r0c2, hfloat32 r0c3,
						hfloat32 r1c0, hfloat32 r1c1, hfloat32 r1c2, hfloat32 r1c3,
						hfloat32 r2c0, hfloat32 r2c1, hfloat32 r2c2, hfloat32 r2c3,
						hfloat32 r3c0, hfloat32 r3c1, hfloat32 r3c2, hfloat32 r3c3 )
	{
		_xbase = _mm_setr_ps( r0c0, r1c0, r2c0, r3c0 );
		_ybase = _mm_setr_ps( r0c1, r1c1, r2c1, r3c1 );
		_zbase = _mm_setr_ps( r0c2, r1c2, r2c2, r3c2 );
		_translation = _mm_setr_ps( r0c3, r1c3, r2c3, r3c3 );
	}

	HMatrix4x4& HMatrix4x4::operator=( const HMatrix4x4& other )
	{
		_xbase = other._xbase;
		_ybase = other._ybase;
		_zbase = other._zbase;
		_translation = other._translation;
		return *this;
	}

	HVector3 HMatrix4x4::operator*( const HVector3& vector )
	{
		//fill 128's with x, y, and z.
		__m128 xcolumn = _mm_mul_ps( _xbase, _mm_shuffle_ps( vector._data, vector._data, _MM_SHUFFLE( 0, 0, 0, 0 ) ) ); 
		__m128 ycolumn = _mm_mul_ps( _ybase, _mm_shuffle_ps( vector._data, vector._data, _MM_SHUFFLE( 1, 1, 1, 1 ) ) );
		__m128 zcolumn = _mm_mul_ps( _zbase, _mm_shuffle_ps( vector._data, vector._data, _MM_SHUFFLE( 2, 2, 2, 2 ) ) );

		// accumulate the values into a single 128.
		__m128 result = _mm_add_ps( xcolumn, ycolumn );
		result = _mm_add_ps( result, zcolumn );
		return result;
	}

	__m128& HMatrix4x4::xBase()
	{
		return _xbase;
	}

	__m128& HMatrix4x4::yBase()
	{
		return _ybase;
	}

	__m128& HMatrix4x4::zBase()
	{
		return _zbase;
	}

	__m128& HMatrix4x4::translation()
	{
		return _translation;
	}

	HVector4 HMatrix4x4::operator*( const HVector4& vector )
	{
		//fill 128's with x, y, z, and translation.
		__m128 xcolumn = _mm_mul_ps( _xbase, _mm_shuffle_ps( vector._data, vector._data, _MM_SHUFFLE( 0, 0, 0, 0 ) ) ); 
		__m128 ycolumn = _mm_mul_ps( _ybase, _mm_shuffle_ps( vector._data, vector._data, _MM_SHUFFLE( 1, 1, 1, 1 ) ) );
		__m128 zcolumn = _mm_mul_ps( _zbase, _mm_shuffle_ps( vector._data, vector._data, _MM_SHUFFLE( 2, 2, 2, 2 ) ) );
		__m128 translationcolumn = _mm_mul_ps( _translation, _mm_shuffle_ps( vector._data, vector._data, _MM_SHUFFLE( 3, 3, 3, 3 ) ) ); 

		// accumulate the values into a single 128.
		__m128 result = _mm_add_ps( xcolumn, ycolumn );
		result = _mm_add_ps( result, zcolumn );
		result = _mm_add_ps( result, translationcolumn );
		return result;
	}

		

	HMatrix4x4 HMatrix4x4::operator*( const HMatrix4x4& other )
	{
		//do the same as vector by matrix, but do it 4 times
		HMatrix4x4 res;
		//1.
		//fill 128's with x, y, z, and translation.
		__m128 xcolumn = _mm_mul_ps( _xbase, _mm_shuffle_ps( other._xbase, other._xbase, _MM_SHUFFLE( 0, 0, 0, 0 ) ) ); 
		__m128 ycolumn = _mm_mul_ps( _ybase, _mm_shuffle_ps( other._xbase, other._xbase, _MM_SHUFFLE( 1, 1, 1, 1 ) ) );
		__m128 zcolumn = _mm_mul_ps( _zbase, _mm_shuffle_ps( other._xbase, other._xbase, _MM_SHUFFLE( 2, 2, 2, 2 ) ) );
		__m128 translationcolumn = _mm_mul_ps( _translation, _mm_shuffle_ps( other._xbase, other._xbase, _MM_SHUFFLE( 3, 3, 3, 3 ) ) ); 

		// accumulate the values into a single 128.
		res._xbase = _mm_add_ps( xcolumn, ycolumn );
		res._xbase = _mm_add_ps( res._xbase, zcolumn );
		res._xbase = _mm_add_ps( res._xbase, translationcolumn );
			

		//2.
		//fill 128's with x, y, z, and translation.
			xcolumn = _mm_mul_ps( _xbase, _mm_shuffle_ps( other._ybase, other._ybase, _MM_SHUFFLE( 0, 0, 0, 0 ) ) ); 
			ycolumn = _mm_mul_ps( _ybase, _mm_shuffle_ps( other._ybase, other._ybase, _MM_SHUFFLE( 1, 1, 1, 1 ) ) );
			zcolumn = _mm_mul_ps( _zbase, _mm_shuffle_ps( other._ybase, other._ybase, _MM_SHUFFLE( 2, 2, 2, 2 ) ) );
			translationcolumn = _mm_mul_ps( _translation, _mm_shuffle_ps( other._ybase, other._ybase, _MM_SHUFFLE( 3, 3, 3, 3 ) ) ); 

		// accumulate the values into a single 128.
		res._ybase = _mm_add_ps( xcolumn, ycolumn );
		res._ybase = _mm_add_ps( res._ybase, zcolumn ); 
		res._ybase = _mm_add_ps( res._ybase, translationcolumn );


		//3.
		//fill 128's with x, y, z, and translation.
			xcolumn = _mm_mul_ps( _xbase, _mm_shuffle_ps( other._zbase, other._zbase, _MM_SHUFFLE( 0, 0, 0, 0 ) ) ); 
			ycolumn = _mm_mul_ps( _ybase, _mm_shuffle_ps( other._zbase, other._zbase, _MM_SHUFFLE( 1, 1, 1, 1 ) ) );
			zcolumn = _mm_mul_ps( _zbase, _mm_shuffle_ps( other._zbase, other._zbase, _MM_SHUFFLE( 2, 2, 2, 2 ) ) );
			translationcolumn = _mm_mul_ps( _translation, _mm_shuffle_ps( other._zbase, other._zbase, _MM_SHUFFLE( 3, 3, 3, 3 ) ) ); 

		// accumulate the values into a single 128.
		res._zbase = _mm_add_ps( xcolumn, ycolumn );
		res._zbase = _mm_add_ps( res._zbase, zcolumn );
		res._zbase = _mm_add_ps( res._zbase, translationcolumn );


		//4.
		//fill 128's with x, y, z, and translation.
		xcolumn = _mm_mul_ps( _xbase, _mm_shuffle_ps( other._translation , other._translation, _MM_SHUFFLE( 0, 0, 0, 0 ) ) ); 
			ycolumn = _mm_mul_ps( _ybase, _mm_shuffle_ps( other._translation, other._translation, _MM_SHUFFLE( 1, 1, 1, 1 ) ) );
			zcolumn = _mm_mul_ps( _zbase, _mm_shuffle_ps( other._translation, other._translation, _MM_SHUFFLE( 2, 2, 2, 2 ) ) );
			translationcolumn = _mm_mul_ps( _translation, _mm_shuffle_ps( other._translation, other._translation, _MM_SHUFFLE( 3, 3, 3, 3 ) ) ); 

		// accumulate the values into a single 128.
		res._translation = _mm_add_ps( xcolumn, ycolumn );
		res._translation = _mm_add_ps( res._translation, zcolumn );
		res._translation = _mm_add_ps( res._translation, translationcolumn );
		return res;
	}

	HMatrix4x4& HMatrix4x4::transpose()
	{
		_MM_TRANSPOSE4_PS(_xbase, _ybase, _zbase, _translation)

		return *this;
	}
}